<?php
namespace App\Repos;

use App\Models\Order;
use App\Models\OrderProduct;


class OrderProductRepo
{
	public static function countByProduct($where=[],$num=10)
	{	   
	  	$day = array_pull($where,'day');
	  	//dd($day);
	  	$query = OrderProduct::where($where);
	  	if($day){
	  		$to = date('Y-m-d');
	  		if($day == 3 ) {
	  			$from =  date('Y-m-d',strtotime("-1 week"));
	  			$query->whereBetween('created_at',[$from,$to]);
	  			    
	  		} elseif($day==2) {
	  			$from = date('Y-m-d',strtotime("-3 day"));
	  			$query->whereBetween('created_at',[$from,$to]);
        		
	  		} else {
	  			$date = date('Y-m-d',strtotime( "yesterday" ));
	  			$query->whereDate('created_at',$date); 
	  		}
	  		unset($where['day']);
	  	}

	  
	  	$list = $query->selectRaw('sum(product_num) as total_num, sum(total_price) as total_price,product_id,product_name,product_thumb')
	  			->groupBy('product_id')
	  			->paginate($num); 
	  	return $list;     
	}

	public static function countByOrder($where)
	{
		
		$today = date('Y-m-d');
		$data['today'] = Order::where($where)->whereDate('created_at',$today)->sum('total_price');

		$yesterday = date('Y-m-d',strtotime( "yesterday" ));
		$data['day_6'] = Order::where($where)->whereDate('created_at',$yesterday)->sum('total_price');


    	$day_5 = date('Y-m-d',strtotime("-2 day"));
    	$data['day_5'] = Order::where($where)->whereDate('created_at',$day_5)->sum('total_price');

    	$day_4 = date('Y-m-d',strtotime("-3 day"));
    	$data['day_4'] = Order::where($where)->whereDate('created_at',$day_4)->sum('total_price');

    	$day_3 = date('Y-m-d',strtotime("-4 day"));
    	$data['day_3'] = Order::where($where)->whereDate('created_at',$day_3)->sum('total_price');

    	$day_2 = date('Y-m-d',strtotime("-5 day"));
    	$data['day_2'] = Order::where($where)->whereDate('created_at',$day_2)->sum('total_price');

    	$day_1 = date('Y-m-d',strtotime("-1 week"));
    	$data['day_1'] = Order::where($where)->whereDate('created_at',$day_1)->sum('total_price');
		return $data;
	}

	public static function find($where)
	{
		return OrderProduct::where($where)->first();
	}

	

	
	
	

	
	
	
}